package com.yanqu.road.dao.impl.activity.biaoju;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.biaoju.BiaoJuTeamUserInfo;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuBestEscortData;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuUserRunEscortData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBiaoJuBestEscortImpl extends TempDataObjectDao<CrossBiaoJuBestEscortData> {
    public static String getCrossBiaoJuBestEscortDataSql = "select * from `t_c_biaoju_best_escort` where `activityId`=? and `unionGroup`=?";

    public Map<Integer, CrossBiaoJuBestEscortData> getCrossBiaoJuBestEscortData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, CrossBiaoJuBestEscortData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossBiaoJuBestEscortDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossBiaoJuBestEscortData data = parse(rs);
                dataMap.put(data.getUniqueId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossBiaoJuBestEscortDataSql = "update `t_c_biaoju_best_escort` set round=?,escortId=?,quality=?,cityId=?,createTime=?,takeUserId=?,takeTime=?,remainNum=?,totalNum=? where activityId=? and uniqueId=? and unionGroup=?";
    @Override
    protected boolean updateData(CrossBiaoJuBestEscortData crossBiaoJuBestEscortData) {
        crossBiaoJuBestEscortData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getEscortId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getQuality()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getCityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuBestEscortData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuBestEscortData.getTakeUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuBestEscortData.getTakeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getRemainNum()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getTotalNum()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getUniqueId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getUnionGroup()));
        if (getDb().executeNoneQuery(updateCrossBiaoJuBestEscortDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossBiaoJuBestEscortDataSql = "insert into `t_c_biaoju_best_escort`(activityId,uniqueId,unionGroup,round,escortId,quality,cityId,createTime,takeUserId,takeTime,remainNum,totalNum) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossBiaoJuBestEscortData crossBiaoJuBestEscortData) {
        crossBiaoJuBestEscortData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getUniqueId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getEscortId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getQuality()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getCityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuBestEscortData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuBestEscortData.getTakeUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuBestEscortData.getTakeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getRemainNum()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuBestEscortData.getTotalNum()));
        if (getDb().executeNoneQuery(insertCrossBiaoJuBestEscortDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossBiaoJuBestEscortData parseData(ResultSet rs) throws SQLException {
        CrossBiaoJuBestEscortData crossBiaoJuBestEscortData = new CrossBiaoJuBestEscortData();
        crossBiaoJuBestEscortData.setActivityId(rs.getInt("activityId"));
        crossBiaoJuBestEscortData.setUniqueId(rs.getInt("uniqueId"));
        crossBiaoJuBestEscortData.setUnionGroup(rs.getInt("unionGroup"));
        crossBiaoJuBestEscortData.setRound(rs.getInt("round"));
        crossBiaoJuBestEscortData.setEscortId(rs.getInt("escortId"));
        crossBiaoJuBestEscortData.setQuality(rs.getInt("quality"));
        crossBiaoJuBestEscortData.setCityId(rs.getInt("cityId"));
        crossBiaoJuBestEscortData.setCreateTime(rs.getLong("createTime"));
        crossBiaoJuBestEscortData.setTakeUserId(rs.getLong("takeUserId"));
        crossBiaoJuBestEscortData.setTakeTime(rs.getLong("takeTime"));
        crossBiaoJuBestEscortData.setRemainNum(rs.getInt("remainNum"));
        crossBiaoJuBestEscortData.setTotalNum(rs.getInt("totalNum"));

        return crossBiaoJuBestEscortData;
    }

}
